package cn.com.bob.messageboard.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.bob.messageboard.domain.Message;
import cn.com.bob.messageboard.service.MessageService;
import cn.com.bob.messageboard.mapper.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 12948
* @description 针对表【message(留言板;)】的数据库操作Service实现
* @createDate 2023-07-11 16:19:20
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Autowired
    private MessageMapper messageMapper;
    /**
     * 添加留言
     * @param userName:
     * @param content:
     * @return void
     */
    @Override
    public void addMsg(String userName,String content) {
        Message message = new Message();
        message.setUsername(userName);
        message.setContent(content);
        message.setCreateTime(new Date());
        message.setUpdateTime(new Date());
        System.out.println(new Date());
        messageMapper.insert(message);
    }

    /**
     * 删除留言
     * @param userId:
     * @param messageId:
     * @return void
     */
    @Override
    public void deleteMsg(String username, Integer messageId) {
        QueryWrapper<Message> msg = new QueryWrapper<Message>().eq("message_id", messageId);
        Message message = messageMapper.selectOne(msg);
        message.setIsDeleted(1);
        message.setDeleteTime(new Date());
        messageMapper.update(message, msg);
    }

    /**
     * 修改留言
     * @param messageId:
     * @param content:
     * @return void
     */
    @Override
    public void updateMsg(Integer messageId,String content) {
        QueryWrapper<Message> msg = new QueryWrapper<Message>().eq("message_id", messageId);
        Message message = messageMapper.selectOne(msg);
        message.setUpdateTime(new Date());
        message.setContent(content);
        messageMapper.update(message,msg);
    }

    /**
     * 查询所有
     * @param :
     * @return List<Message>
     */
    @Override
    public List<Message> getAllMsg() {
        List<Message> messages = messageMapper.selectList(new QueryWrapper<Message>().eq("is_deleted", 0));
        List<Message> res = new ArrayList<>();
        for (Message m :
                messages) {
            Message message = new Message();
            message.setUsername(m.getUsername());
            message.setContent(m.getContent());
            message.setCreateTime(m.getCreateTime());
            res.add(message);
        }
        return  res;
    }

    /**
     * 分页查询
     *
     * @param pageNum:  页码
     * @param pageSize: 每页显示条数
     * @return Page<Message>
     */
    @Override
    public Page<Message> getMsgByPage(int pageNum, int pageSize) {
        Page<Message> page = new Page<>(pageNum, pageSize);

        // 创建查询条件
        QueryWrapper<Message> queryWrapper = new QueryWrapper<Message>().eq("is_deleted", 0);
        queryWrapper.orderByDesc("create_time");
        // 可以根据需要添加其他查询条件
        // queryWrapper.eq("status", 1);

        // 执行分页查询
        messageMapper.selectPage(page, queryWrapper);

        return page;
    }

    /**
     * 根据内容关键词实现模糊查询
     * @param content:
     * @return List<Message>
     */
    @Override
    public List<Message> getMsgByName(String content) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<Message>().like("content", content);
        queryWrapper.orderByDesc("create_time");
        List<Message> messageList = messageMapper.selectList(queryWrapper);
        return  messageList;
    }
}




